//使用koa复写入口文件
const Koa = require('koa');
const Router = require('koa-router');
const koaBody = require('koa-body');
const views = require('koa-views');
const serve = require('koa-static');
const session = require('koa-session');
const staticCache = require('koa-static-cache');
const jwt = require('jsonwebtoken')

const app = new Koa();
const router = new Router();
app.keys = ['wopelo'];

// jwt秘钥
const jwtSecret = 'secretWopeloDouban'

const config = {
	key: 'wopelo',
	maxAge: 10*1000
};
app.use(session(config, app));

app.use(serve(__dirname));
app.use(views(__dirname + '/views/pages',{
	extension: 'pug'
}))
app.use(koaBody()).use(router.routes());
app.use(staticCache(__dirname + '/image', {
	maxAge: 5*60
}))

app.listen(3000);

function backClient(ctx,result){
	ctx.response.type = 'json';
	ctx.response.body = result;
}

router.get('/', async(ctx, next) => {
	let jack = ctx.cookies.get('jack',{signed: true});
	console.log(jack)
	if(!jack){
		ctx.cookies.set('jack', "value-content",{maxAge: 10*1000,signed: true,httpOnly: false});
		console.log("添加jack");
	}else{
		console.log(jack);
	}

	let n = ctx.session.views || 0;
	ctx.session.views = ++n;
	console.log("sess.views:",ctx.session.views);

	await ctx.render("index",{
		title:"nodeWeb 首页"
	})
});

// router.get('/other', async(ctx) => {
// 	ctx.set("Location",'/');
// });

router.get("/movie/:id", async(ctx, next) => {
	await ctx.render("detail",{
		title:"nodeWeb 详情页"
	});
});

router.get("/admin/", async(ctx, next) => {
	await ctx.render("admin",{
		title:"nodeWeb 后台管理页"
	});
});

router.get("/admin/movie", async(ctx, next) => {
	await ctx.render("movie",{
		title:"nodeWeb 影片管理页"
	});
});

router.get("/admin/list", async(ctx, next) => {
	await ctx.render("list",{
		title:"nodeWeb 用户管理页"
	});
});

router.post("/server/findAll", async(ctx, next) => {
	let findAll = require("./server/findAll.js");
	const token = ctx.request.header.authorization;
	jwt.verify(token, jwtSecret, function (err, decode) {
		if (err) {        
			console.log('err', err.message)    
		} else {
			console.log('success', decode)
		}
	})
	await findAll().then((result) => {
		backClient(ctx,result);
	})
});

router.post("/server/target", async(ctx, next) => {
	let target = require("./server/target.js");
	let body = ctx.request.body;
	await target(body.num).then((result) => {
		backClient(ctx,result);
	});
});

router.post("/server/haveUser", async(ctx, next) => {
	let haveUser = require("./server/haveUser.js");
	let body = ctx.request.body;
	console.log('ip', ctx.request.ip)
	await haveUser(body.name, body.password, jwtSecret).then((result) => {
		const data = {
			_id: result[0]._id,
			name: result[0].name,
			identity: result[0].identity,
			head: result[0].head,
			token: jwt.sign({
				id: result[0]._id
			}, jwtSecret, {
				expiresIn: 60  // 1分钟过期
			})
		}
		backClient(ctx, [data]);
	})
});

router.post("/server/register", async(ctx, next) => {
	let register = require("./server/register.js");
	let body = ctx.request.body;
	await register(body.name, body.password).then((result) => {
		backClient(ctx,result);
	});
});